/**
 * 
 */
package sqlToGUI.charts;

import javax.swing.JPanel;


import java.awt.Container;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.jfree.data.general.DefaultValueDataset;

import sqlToGUI.charts.helperUtilities.TelemetryChartEvent;
import sqlToGUI.charts.helperUtilities.TelemetryChartListener;
import sqlToGUI.charts.helperUtilities.TelemetryDataEntry;

/**
 * @author Maciej
 *
 */
@SuppressWarnings("serial")
public class BarChartPanel extends JPanel implements GUIDisplay{

	/**The list receiving the new data from the outside -
	 * This class will be responsible for listening to this
	 * list and updating from it whenever new information
	 * is available -
	 * The array is used in case chart uses reference to more
	 * than one data set and is updated from more than one
	 * list */
	private TelemetryDataEntry [] dataEntries;
	
	private double buffer;
	private Color color;
	private BufferedImage img = null;
	private String displayData;
	
	private String chartName;
	private int min;
	private int max;
	
	/**The log panel data will monitor the incoming data */
	private LogPanel log;
	/**Variable indicating whether or not a log has been
	 * registered */
	private boolean logReg = false;
	
	public BarChartPanel(Color c, BufferedImage icon, String chartName) {
		super();
		//this.buffer = new DefaultValueDataset();
		this.color = c;
		this.img = icon;
		this.chartName = chartName;
		
		initChartParams();
	}
	
	private void initChartParams() {
		Properties barChartParams = new Properties();
		try {
			barChartParams.load(new FileInputStream
					("properties/barChartParam.properties"));
		} catch (FileNotFoundException e) {
			System.out.println("Bar Chart: Failed to load property file");
			System.exit(1);
		} catch (IOException e) {
			System.out.println("Bar Chart: Failed to load property file");
			System.exit(1);
		}
	
		//min = Integer.parseInt(barChartParams.getProperty(chartName + "Min"));
		//max = Integer.parseInt(barChartParams.getProperty(chartName + "Max"));
	}

	/**Create the display chart */
	public void paintComponent(Graphics g) {
		g.setColor(color);
		int height = this.getHeight();
		int width = this.getWidth();
		
		
		
		g.fillRect(0, 0, width, height);

		g.setColor(Color.black);
		//g.setFont()
		g.drawString("123xx/x", 0, height - 10);
		g.drawImage(img, 0 , 0, this);

	}

	/**Returns the chart that plots the data */
	public Container getChart() {
		return this;
	}

	/**Register the given log so that it can monitor incoming data */
	public GUIDisplay registerLog(LogPanel log){
		this.log = log;
		logReg = true;
		return this;
	}
	
	/**Moved the frame for which the data will be displayed on the chart */
	public void addToBuffer(int index, double value) {
		//buffer.setValue(value);
		buffer = value;
		this.repaint();
	}

	@Override
	/**Return the buffer for the chart */
	public TelemetryDataEntry [] getBuffers() {
		
		dataEntries = new TelemetryDataEntry [1];
		for(int i = 0; i < dataEntries.length; i++)
			dataEntries[i] = new TelemetryDataEntry(1);

		/**Create the event(update chart) when this buffer receives new data */
		dataEntries[0].addTelemetryChartEventListener(new TelemetryChartListener() {
			public void myEventOccurred(TelemetryChartEvent evt) {
				addToBuffer(0, dataEntries[0].getUpdate()[0]);
				//paintComponent(getGraphics());
			}
		});

		/**TODO: send data to the log*/
		
		return dataEntries;
	}

}
